package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.R;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * Created by ZhaoJun on 2021/11/28 15:03
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String number, String beginTime, String endTime) throws ParseException {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        if(beginTime!=null){
//            Date beginDate = sdf.parse(beginTime);
            lqw.ge(Orders::getOrderTime,beginTime);
        }
        if(endTime!=null){
//            Date endDate = sdf.parse(endTime);
            lqw.le(Orders::getOrderTime,endTime);
        }

        lqw.like(number != null,Orders::getNumber, number);
        orderService.page(pageInfo, lqw);
        pageInfo.getRecords().stream().forEach(c->{

        });

        return R.success(pageInfo);


    }

    @PutMapping
    public R status(@RequestBody Map map){
        Integer status = (Integer)map.get("status");
        String id = (String)map.get("id");
        Orders orders = new Orders();
        orders.setStatus(status);
        orders.setId(Long.valueOf(id));

        orderService.updateById(orders);
        return R.success("状态修改成功");
    }
    @GetMapping("/userPage")
    public R page(Integer page, Integer pageSize) {
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }


















}
